#!/usr/bin/env python
# Settings for vim: fileencoding=utf-8 shiftwidth=2 tabstop=2 expandtab


import urllib2
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext import db

class MainHandler(webapp.RequestHandler):

  def get(self):
    self.response.out.write('<html><body style="padding:0px; margin:0px">')
    self.response.out.write('<textarea style="padding:0px; margin:0px; overflow:visible" rows="1" cols="20">foo</textarea>')
    self.response.out.write('</body></html>')
    return
    self.response.out.write('<html><head><title>fetch</title></head><body>')
    self.response.out.write('\n<button onclick="fetch()">fetch</button>')
    self.response.out.write('\n<button onclick="peek()">peek</button>')
    self.response.out.write('''<script type="text/javascript">
      function fetch() {
        alert("arf!");
      }
      function peek() {
        alert("I see you!");
      }
    </script>''')
    self.response.out.write('''<form action="/" method="post">
    <input type="submit" value="Submit" />
    <textarea name="data" rows="10" cols="80" />
    </form>''')
    self.response.out.write('\n</body></html>\n')
  def post(self):
    storage = ReceivedData.all().fetch(1)
    if len(storage) > 0:
      storage = storage[0]
    else:
      storage = ReceivedData()
    storage.data = self.request.get("data")
    storage.put()

class ReceivedData(db.Model):
  data = db.TextProperty()

def main():
  application = webapp.WSGIApplication([('/', MainHandler)], debug=True)
  util.run_wsgi_app(application)

if __name__ == '__main__':
  main()
